package com.example.echoewish.ui.main_activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.HotSearch;
import com.example.echoewish.repository.PostMessageRepository;
import com.example.echoewish.ui.home_activity.PostDetailActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HotSearchRankingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HotSearchRankingFragment extends Fragment {
    private final  static int CHANGE_LIST_VIEW=3;
    private final  static int NO_CHANGE_LIST_VIEW=4;
    private ListView listView;
    private HotSearchRankAdapter hotSearchRankAdapter;
    private SearchContentViewModel searchContentViewModel;

    private Handler hotSearchRankingFragmentHandler=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case  Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    if(msg.arg1==CHANGE_LIST_VIEW)
                    {
                        hotSearchRankAdapter.clear();
                        hotSearchRankAdapter.addAll(searchContentViewModel.hotSearchMutableLiveData.getValue());
                        hotSearchRankAdapter.notifyDataSetChanged();
                    }
                    else if(msg.arg1==NO_CHANGE_LIST_VIEW)
                        if (getActivity()!=null)
                            Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
                    break;
                case  Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    if (getActivity()!=null)
                        Toast.makeText(getActivity(),"网络错误，请检测网络",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    public HotSearchRankingFragment() {
        // Required empty public constructor
    }

    public static HotSearchRankingFragment newInstance() {
        HotSearchRankingFragment fragment = new HotSearchRankingFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initViewModel();
        observeViewModel();
        View view=inflater.inflate(R.layout.fragment_hot_search_ranking, container, false);
        listView=view.findViewById(R.id.hot_search_rank_listview);
        hotSearchRankAdapter=new HotSearchRankAdapter(getActivity(),R.layout.hot_search_rank_item,new LinkedList<>());
        listView.setAdapter(hotSearchRankAdapter);
        hotSearchRankAdapter.setTextClickListener(new HotSearchRankAdapter.TextClickListener() {
            @Override
            public void onClick(int position) {
                int postId=searchContentViewModel.hotSearchMutableLiveData.getValue().get(position).postId;
                Intent intent=new Intent(HotSearchRankingFragment.this.getActivity(), PostDetailActivity.class);
                intent.putExtra("postId",postId);
                HotSearchRankingFragment.this.getActivity().startActivity(intent);
            }
        });
        return view;
    }

    private void initViewModel()
    {
        searchContentViewModel=new ViewModelProvider(getActivity(),new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(SearchContentViewModel.class);
    }

    private void observeViewModel()
    {
        searchContentViewModel.hotSearchMutableLiveData.observe(getActivity(), new Observer<List<HotSearch>>() {
            @Override
            public void onChanged(List<HotSearch> hotSearches) {
                Message message=new Message();
                message.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                if(hotSearches!=null&&hotSearches.size()!=0)
                {
                    message.arg1= CHANGE_LIST_VIEW;
                }
                else
                    message.arg1=NO_CHANGE_LIST_VIEW;
                hotSearchRankingFragmentHandler.sendMessage(message);
            }
        });
    }
}